<template>
  <div>
    <van-nav-bar
      class="link"
      title="账号登录"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="username" placeholder="请输入账号" />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>

    <div class="reg">
      <router-link to="/register">还没有账号，去注册~</router-link>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (val) {
      // console.log('onSubmit', val)
      const res = await login(val)
      if (res.status === 200) {
        setToken(res.body.token)
        this.$toast.success('登录成功')
        this.$router.push('/')
      } else {
        this.$toast.success('你的账号或者密码错误')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  background-color: #21b97a;
}
::v-deep .van-nav-bar__title,
.van-ellipsis {
  color: #fff;
  font-size: 18px;
  font-weight: 400;
}
::v-deep .van-icon,
.van-icon-arrow-left,
.van-nav-bar__arrow {
  color: #fff;
}
.van-cell,
.van-field {
  height: 60px;
  font-size: 17px;
  line-height: 60px;
}
// ::v-deep .van-button,
// .van-button--info,
// .van-button--normal,
// .van-button--block {
//   font-style: 18px !important;
// }
.reg {
  // margin: 0 auto;
  text-align: center;
}
.reg a {
  color: #666;
  font-size: 14px;
}
</style>
